load MAT/WIOD.mat;
load('MAT/Step_2_firm_data_alpha4.mat');

NT_sectors={'E','F','50','51','52','H','60','61','62','63','64',...
                        'J','70','71t74','L','M','N','O','P'}';
WIOD_sectors = {'AtB' , 'C' , '15t16' ,'17t18' , '19' , '20' , '21t22' ,...
    '23' , '24' , '25' ,'26', '27t28', '29' , '30t33' , '34t35' ,...
    '36t37', 'E' , 'F' , '50', '51', '52', 'H', '60', '61', '62', '63',...
    '64', '70', '71t74', 'M', 'N', 'O'}';                     
T = ones(FI_J,1);
for i =1:J
    for j=1:size(NT_sectors,1)
        if isequal(WIOD_sectors{i},NT_sectors{j})
            T(start_sorted(i):start_sorted(i+1)-1,1)=0;
        end
    end
end
w = VA_f_Stata/sum(VA_f_Stata);
bin=200;
Share_IM = 1-sum(GAMMA_sorted_matrix(:,start(france):start(france+1)-1),2);
Share_IM_cdf = histcounts(Share_IM,bin,'Normalization','cdf');
[share_bin,share_edge]=discretize(Share_IM,bin);
for i=1:bin;
    w_pdf(:,i) = sum(w(share_bin==i));
end
w_cdf = cumsum(w_pdf);

figure(1)
figure1 = figure;
plot(share_edge(2:end),Share_IM_cdf,'r-',share_edge(2:end),w_cdf,'bo','LineWidth',2)
legend('Unweighted','Weighted','Location','SouthEast');
xlabel('Foreign Inputs Share','Interpreter','Latex');
ylabel('Cumulated Share of Firms','Interpreter','Latex');
axis([0 1 0 1])
saveas(gcf,'Figures/Fig1b.eps','epsc2');

'Share non import (unweighted and weighted)'
Share_IM_cdf(1,1)
w_cdf(1,1)
'Share export sales >50% (unweighted and weighted)'
1-Share_IM_cdf(1,80)
1-w_cdf(1,80)

eval([strcat('load MAT/rho',rhoT,'eta',etaT,'lambda',lambdaT,'_psi',num2str(varphi),'_sigma',num2str(sigmaT),'/Step_4_setup_alpha',num2str(alphaT),...
         '_rho',rhoT,'_approx.mat')]); 
X_mnjf = firmsecD_sorted*(X_mnj(start(france):start(france+1)-1,:));
X_flows = pi_mnjf.*X_mnjf;
turnover_f = sum(X_flows,2);
exposure_X_f = X_flows./repmat(turnover_f,1,N);
exposure_X_f_T = exposure_X_f(T==1,:);

Share_EX = 1-exposure_X_f_T(:,france);
Share_EX_cdf = histcounts(Share_EX,bin,'Normalization','cdf');
bin=200;
[share_bin,share_edge]=discretize(Share_EX,bin);

for i=1:bin;
    w_pdf(:,i) = sum(w(share_bin==i));
end
w_pdf = w_pdf/sum(w_pdf);
w_cdf = cumsum(w_pdf);

figure(1)
plot(share_edge(2:end),Share_EX_cdf,'r-',share_edge(2:end),w_cdf,'bo','LineWidth',2)
legend('Unweighted','Weighted','Location','SouthEast');
xlabel('Foreign Sales Share','Interpreter','Latex');
ylabel('Cumulated Share of Firms','Interpreter','Latex');
axis([0 1 0 1])
saveas(gcf,'Figures/Fig1a.eps','epsc2');

'Share non export'
Share_EX_cdf(1,1)
'Share export sales >50% (unweighted and weighted)'
1-Share_EX_cdf(1,100)
1-w_cdf(1,100)